package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwbpT;
import cn.e4j.modular.crm.service.GwbpTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 业务人员指定客户设置
 * Created by miaoyl on 2019-1-16.
 */
@Controller
@RequestMapping("/crm/gwai303")
public class Gwai303Controller extends BaseController {

    @Autowired
    private GwbpTService gwbpTService;

    /**
     * 跳转业务人员指定客户设置页面
     */
    @RequiresPermissions("crm:gwai303:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai303.html";
    }

    /**
     * 查询业务人员指定客户信息列表
     */
    @RequestMapping("/getGwbpList")
    @RequiresPermissions("crm:gwai303:init")
    @ResponseBody
    public ResponseData getGwbpList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ent",shiroUser.getDBC());
        inDto.put("lang",LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwbpTService.getGwbpList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询业务人员开窗信息列表
     */
    @RequestMapping("/getUserList")
    @ResponseBody
    public ResponseData getUserList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbpTService.getUserList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询客户开窗信息列表
     */
    @RequestMapping("/getGwakList")
    @ResponseBody
    public ResponseData getGwakList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ent",shiroUser.getDBC());
        inDto.put("lang",LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwbpTService.getGwakList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai303:init")
    @ResponseBody
    public ResponseData add(GwbpT gwbpT) {
        // 判断key值是否重复
        GwbpT theGwbpT = gwbpTService.getByKey(gwbpT);
        if (theGwbpT != null) {
            //throw new ServiceException(CrmExceptionEnum.EXISTED_THE_CODE);
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        List<Map<String, Object>> result = gwbpTService.getGwaa(gwbpT.getGWBP001());
        if(("Y").equals(gwbpT.getGWBP003()) && result.size() == 0){
            return new ErrorResponseData(CrmExceptionEnum.EMPTY_THE_GWAA.getCode() ,
                    CrmExceptionEnum.EMPTY_THE_GWAA.getMessage());
        }
        gwbpTService.add(gwbpT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai303:init")
    @ResponseBody
    public ResponseData edit(GwbpT gwbpT,@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        GwbpT oldGwbpT = new GwbpT();
        oldGwbpT.setGWBP001(inDto.getString("oldGWBP001"));
        oldGwbpT.setGWBP002(inDto.getString("oldGWBP002"));
        if(!(oldGwbpT.getGWBP001().equals(gwbpT.getGWBP001()) && oldGwbpT.getGWBP002().equals(gwbpT.getGWBP002()))) {
            GwbpT theGwbpT = gwbpTService.getByKey(gwbpT);
            if (theGwbpT != null) {
                return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                        CrmExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
        }
        List<Map<String, Object>> result = gwbpTService.getGwaa(gwbpT.getGWBP001());
        if(("Y").equals(gwbpT.getGWBP003()) && result.size() == 0){
            return new ErrorResponseData(CrmExceptionEnum.EMPTY_THE_GWAA.getCode() ,
                    CrmExceptionEnum.EMPTY_THE_GWAA.getMessage());
        }
        gwbpTService.edit(gwbpT,oldGwbpT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai303:init")
    @ResponseBody
    public ResponseData del(GwbpT gwbpT) {
        gwbpTService.delete(gwbpT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwai303:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbpT> gwbpTList = inDto.getReqList(GwbpT.class);
        gwbpTService.delDataBatch(gwbpTList);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/exsitGwaa")
    @ResponseBody
    public ResponseData exsitGwaa(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbpTService.getGwaa(inDto.getString("GWAA"));
        return SUCCESS_TIP;
    }
}
